﻿Imports System.Data.SqlClient
Public Class FrmThietLap

    Private Sub FrmThietLap_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If ThietLap("Thang") <> "" Then
            cbxNam.Items.Add(ThietLap("Nam"))
            cbxNam.SelectedIndex = 0
            cbxThang.Text = ThietLap("Thang")
            cbxThang.Enabled = False
            cbxNam.Enabled = False
        Else
            For i As Integer = Now.Year - 5 To Now.Year + 5
                cbxNam.Items.Add(i)
            Next
            cbxNam.Text = Now.Year
            cbxThang.Text = Now.Month
        End If
        Me.txtTenCT.Text = ThietLap("CFG01")
        Me.txtDiaChi.Text = ThietLap("CFG02")
        Me.txtSoDt.Text = ThietLap("CFG03")
        Me.txtMSThue.Text = ThietLap("CFG04")
        Me.txtTKNganHang.Text = ThietLap("CFG05")
        Me.txtGiamDoc.Text = ThietLap("CFG06")
        Me.txtThuTruong.Text = ThietLap("CFG07")
        Me.txtKTT.Text = ThietLap("CFG08")
        Me.txtNguoiLap.Text = ThietLap("CFG09")
    End Sub

    Private Sub btnLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLuu.Click
        If validateInput() Then
            If CapNhat() Then
                cbxThang.Enabled = False
                cbxNam.Enabled = False
                MsgBox("Cập nhật thành công.")
            Else
                MsgBox("Cập nhật thất bại.")
            End If
        End If

    End Sub

    Private Sub btnHuyBo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDong.Click
        Me.Close()
    End Sub

    Private Function CapNhatKyHan() As Boolean
        Dim strSql As String = ""
        Dim myConn As New SqlConnection(chuoiketnoi)
        Dim myComm As New SqlCommand
        Try
            myConn.Open()
            myComm.Connection = myConn
            strSql = "Update ThietLap set GiaTri = @GiaTri where TuKhoa = @TuKhoa"
            myComm.CommandText = strSql
            updateValue(cbxThang.Text, "Thang", myComm)
            updateValue(cbxNam.Text, "Nam", myComm)
            Dim thang As Integer = CInt(cbxThang.Text)
            Dim nam As Integer = CInt(cbxNam.Text)
            If thang = 1 Then
                thang = 12
                nam = nam - 1
            Else
                thang = thang - 1
            End If
            strSql = "insert into KhoaSo(Thang, Nam) values(@Thang, @Nam)"
            myComm.CommandText = strSql
            myComm.Parameters.Clear()
            myComm.Parameters.AddWithValue("@Thang", thang)
            myComm.Parameters.AddWithValue("@Nam", nam)
            myComm.ExecuteNonQuery()

            strSql = "select MaHH from HangHoa"
            Dim myAdapter As New SqlDataAdapter(strSql, myConn)
            Dim myTable As New DataTable
            myAdapter.Fill(myTable)

            strSql = "Insert into CTHangHoa (MaHH, Thang, Nam, SLTonDK, GTTonDK, GiaVonDK, KhoaSo) values (@MaHH, @Thang, @Nam, 0, 0, 0, 0)"
            myComm.CommandText = strSql
            For Each row As DataRow In myTable.Rows
                myComm.Parameters.Clear()
                myComm.Parameters.AddWithValue("@MaHH", row(0))
                myComm.Parameters.AddWithValue("@Thang", ThietLap("Thang"))
                myComm.Parameters.AddWithValue("@Nam", ThietLap("Nam"))
                myComm.ExecuteNonQuery()
            Next

        Catch ex As Exception
            Return False
        End Try
        myConn.Close()
        myConn.Dispose()
        Return True
    End Function

    Private Function CapNhat() As Boolean
        Dim result As Boolean = True
        If ThietLap("Thang") = "" Then
            result = CapNhatKyHan()
        End If
        Dim strSql As String = ""
        Dim myConn As New SqlConnection(chuoiketnoi)
        Dim myComm As New SqlCommand
        Try
            myConn.Open()
            myComm.Connection = myConn
            strSql = "Update ThietLap set GiaTri = @GiaTri where TuKhoa = @TuKhoa"
            myComm.CommandText = strSql
            updateValue(txtTenCT.Text, "CFG01", myComm)
            updateValue(txtDiaChi.Text, "CFG02", myComm)
            updateValue(txtSoDt.Text, "CFG03", myComm)
            updateValue(txtMSThue.Text, "CFG04", myComm)
            updateValue(txtTKNganHang.Text, "CFG05", myComm)
            updateValue(txtGiamDoc.Text, "CFG06", myComm)
            updateValue(txtThuTruong.Text, "CFG07", myComm)
            updateValue(txtKTT.Text, "CFG08", myComm)
            updateValue(txtNguoiLap.Text, "CFG09", myComm)

        Catch ex As Exception
            Return False
        End Try
        myConn.Close()
        myConn.Dispose()
        Return True
    End Function

    Private Sub updateValue(ByVal value As String, ByVal key As String, ByVal mycomm As SqlCommand)
        value = value.Trim()
        If value <> ThietLap(key) Then
            mycomm.Parameters.Clear()
            mycomm.Parameters.AddWithValue("@TuKhoa", key)
            mycomm.Parameters.AddWithValue("@GiaTri", LayDuLieu(value))
            mycomm.ExecuteNonQuery()
            ThietLap(key) = value
        End If
    End Sub

    Function LayDuLieu(ByVal value As String) As Object
        If value = "" Then
            Return DBNull.Value
        End If
        Return value
    End Function

    Private Function validateInput() As Boolean
        If Me.txtTenCT.Text.Trim() = "" Then
            MsgBox("Vui lòng nhập tên công ty.")
            Me.txtTenCT.Focus()
            Return False
        End If
        Return True
    End Function
End Class